﻿using System.Collections.Generic;

using UnityEngine;

/// <summary>
/// UGUI通用组件：字体集合
/// </summary>
[CreateAssetMenu]
public sealed class UGUIFontCollection : ScriptableObject
{
    #region Editor Fields
    [SerializeField, Header("所有字体（不可重名）")]
    private List<Font> _allFonts;
    #endregion

    #region Fields
    /// <summary>
    /// 所有字体（按字体名称为key构成的字典）
    /// </summary>
    public Dictionary<string, Font> FontDict { get { return _fontDict; } }

    private bool _inited;
    private Dictionary<string, Font> _fontDict = new Dictionary<string, Font>();
    #endregion

    private void Init()
    {
        _inited = true;

        if (_allFonts != null)
        {
            for (int i = 0; i < _allFonts.Count; i++)
            {
                var font = _allFonts[i];
                _fontDict.Add(font.name, font);
            }
        }
    }

    public Font GetFont(string fontName)
    {
        if (!_inited) { Init(); }

        if (string.IsNullOrEmpty(fontName))
        {
            GLog.Error("UI", "[UGUIFontCollection] GetFont: fontName is null or empty.");
            return null;
        }

        Font font;

        if (_fontDict.TryGetValue(fontName, out font))
        {
            return font;
        }

        return null;
    }
}